package com.kaili.tms.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.tms.pojo.dto.VehicleRouteAddDTO;
import com.kaili.tms.pojo.dto.VehicleRouteQueryDTO;
import com.kaili.tms.pojo.dto.VehicleRouteUpdateDTO;
import com.kaili.tms.pojo.entity.VehicleRouteAdd;
import com.kaili.tms.pojo.entity.VehicleRouteDelete;
import com.kaili.tms.pojo.entity.VehicleRouteQuery;
import com.kaili.tms.pojo.entity.VehicleRouteUpdate;
import com.kaili.tms.pojo.vo.VehicleRouteVO;
import com.kaili.tms.service.VehicleRouteService;
import com.kaili.tms.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "车线信息", description = "车线信息相关API")
public class VehicleRouteController {

    @Autowired
    private VehicleRouteService vehicleRouteService;

    @GetMapping(value = "/vehicleRoute")
    @ApiOperation(value = "分页查询车线信息")
    public Page<VehicleRouteVO> queryVehicleRoutes(@ModelAttribute VehicleRouteQueryDTO vehicleRouteQueryDTO) {
        VehicleRouteQuery vehicleRouteQuery = new VehicleRouteQuery();
        BeanUtils.copyProperties(vehicleRouteQueryDTO, vehicleRouteQuery);
        Page<VehicleRouteVO> vehicleRouteVOPage = new Page<>(vehicleRouteQuery.getPage(), vehicleRouteQuery.getPageSize());
        return vehicleRouteService.queryVehicleRoutes(vehicleRouteVOPage, vehicleRouteQuery);
    }

    @GetMapping(value = "/vehicleRoute/routeCode")
    @ApiOperation(value = "获取车线code")
    public String getRouteCode() {
        return vehicleRouteService.getRouteCode();
    }

    @PostMapping(value = "/vehicleRoute")
    @ApiOperation(value = "增加车线信息")
    public RestStatus vehicleRoute(@RequestBody VehicleRouteAddDTO vehicleRouteAddDTO) {
        VehicleRouteAdd vehicleRouteAdd = new VehicleRouteAdd();
        BeanUtils.copyProperties(vehicleRouteAddDTO, vehicleRouteAdd);
        vehicleRouteAdd.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
        vehicleRouteAdd.setCreateTime(new Date());
        vehicleRouteService.addVehicleRoute(vehicleRouteAdd);
        vehicleRouteService.addVehicleRouteCars(vehicleRouteAdd.getRouteId(),vehicleRouteAdd.getCarIds());
        return CommonStatus.ADD_OK;
    }

    @PutMapping("/vehicleRoute")
    @ApiOperation(value = "更新车线信息")
    public RestStatus updateVehicleRoute(@Validated @RequestBody VehicleRouteUpdateDTO vehicleRouteUpdateDTO){
        VehicleRouteUpdate vehicleRouteUpdate = new VehicleRouteUpdate();
        BeanUtils.copyProperties(vehicleRouteUpdateDTO, vehicleRouteUpdate);
        vehicleRouteUpdate.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
        vehicleRouteUpdate.setModifyTime(new Date());
        vehicleRouteService.updateVehicleRoute(vehicleRouteUpdate);
        vehicleRouteService.deleteVehicleRouteCars(vehicleRouteUpdate.getRouteId());
        vehicleRouteService.addVehicleRouteCars(vehicleRouteUpdate.getRouteId(),vehicleRouteUpdate.getCarIds());
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/vehicleRoute/{routeId}")
    @ApiOperation(value = "删除车线信息")
    public RestStatus deleteVehicleRoute(@PathVariable Integer routeId) {
        VehicleRouteDelete vehicleRouteDelete = new VehicleRouteDelete();
        if (vehicleRouteService.getVehicleRouteById(routeId) != null){
            vehicleRouteDelete.setRouteId(routeId);
            vehicleRouteDelete.setStatus(1);
            vehicleRouteDelete.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
            vehicleRouteDelete.setModifyTime(new Date());
            vehicleRouteService.deleteVehicleRoute(vehicleRouteDelete);
        }
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/vehicleRoute/{routeId}")
    @ApiOperation(value = "根据id查询单条车线信息")
    public VehicleRouteVO queryVehicleRouteById(@PathVariable Integer routeId) {
        return vehicleRouteService.getVehicleRouteById(routeId);
    }

}
